﻿//段子提交
function articleSubmit() {

    var un = $("#userName").val();

    if (un != null && un != "") {

        setCookie("userName", un);
    }
    $("#articleFormF").get(0).submit();
}
//新文章加入标签
function pushTag(evt) {
    $("#tagStr").val(($("#tagStr").val().trim()));
    var e = $($.event.fix(evt).target);

    if (e.get(0).tagName != "LABEL") {
        e = e.parents("label");
    }
    if (e.length == 1 && e.get(0).tagName == "LABEL") {
        var str = $("#tagStr").val();
        if (str != "") {
            str += ","
        }
        str += e.text();
        $("#tagStr").val(str);
    }
}
//显示回复表单
function showReply(it, id) {
    $("#replyForm").attr("aID", id)
    $(it).parents(".article").append($("#replyForm").show())

}
//提交回复
function submitReply() {
    var un = $("#replyUserName").val();
    if (un != null && un != "") {
        setCookie("userName", un);
    }

    $.post("/a/ar", { userID: $("#replyUserID").val(), userName: $("#replyUserName").val(), replyContent: $("#replyContent").val(), aID: $("#replyForm").attr("aID") }, function (json) {
        if (json.result == "SUCCESS") {
            //alert($("#replyForm").parent().html())
            $("#replyForm").parent(".article").find(".replys").append("<li>@@" + json.content["userName"] + "：" + json.content["content"] + "</li>")
        } else {
            alert(json.content);
        }
    })
}

window.onload = function () {
    //if (ImagePasterLoaded) {
    //    //加载CKEditor编辑器
    //    CKEDITOR.replace('content',
    //    {
    //        extraPlugins: 'imagepaster',
    //        toolbar:
    //        [
    //            ['Bold', 'Italic', '-', 'BulletedList', '-', 'Link', 'Unlink', '-', 'Source', 'Image', 'Maximize'],
    //            ['imagepaster']
    //        ]
    //        , toolbarLocation: "bottom"
    //        , height: 100
	//    , filebrowserImageUploadUrl: "/a/TPCK/"
    //    });
    //} else {

    //}

    //try {
    //    pasterMgr.Init(CKEDITOR.instances.content);
    //} catch (e) {
    //    //加载失败

    //    ImagePasterLoaded = false;
    //}



    var un = getCookie("userName")
    if (un != null && un != "") {
        $("#userName").val(un);
        $("#replyUserName").val(un);
    }
};
//得到更多的a
function getMore() {
    $.post("/a/getMore", { pageIndex: pageIndex++ }, function (json) {
        if (json.result == "SUCCESS") {
            var htmlstr = "";
            for (var i = 0; i < json.content; i++) {
                htmlstr += "";
                ////////////////////////////没写完
            }

        }
    })
}

//写cookies
function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
//读取cookies
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();

    
}

//修改段子


function editA(id) {
    $.post("/a/GA/"+id,function(json){
        if (json.result == "SUCCESS") {

            $('#articleForm').show();

            $("#aID").val(json.content["id"]);
            CKEDITOR.instances.content.setData(json.content["content"]);

            $("#title").val(json.content["title"]);
            $("#datetime").val(json.content["datetime"]);
            $("#userID").val(json.content["userID"]);
            $("#userName").val(json.content["userName"]);
            if (json.content["tags"] != null) {
                $("#tagStr").val(json.content["tags"].join(","));
            }
            $("#from").val(json.content["from"]);
            
        }
    })
}